﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using WP7Client.ServerManager;
using WP7Client.Helpers;
using MSH.Common.DAO;
using WP7Client.UserInterface.Pages;
using MSH.Common.DAO.Results;
using GalaSoft.MvvmLight.Command;
using Microsoft.Phone.Controls.Maps.Platform;
using MSH.Common.Classes;
using System.Globalization;

namespace WP7Client.Controller
{
    public class FriendNotificationController : BaseController
    {
        #region Members

        private Notification notification;
        private FriendNotificationPage friendNotificationPage;
        private String username;

        #endregion

        public FriendNotificationController()
        {
            SaveNotificationCommand = new RelayCommand(Save);
        }

        #region Get&Set

        public Notification Notification
        {
            get { return notification; }
            set 
            { 
                notification = value; 
                OnPropertyChanged("Notification"); 
            }
        }

        public FriendNotificationPage FriendNotificationPage
        {
            get { return friendNotificationPage; }
            set { friendNotificationPage = value; }
        }

        public ICommand SaveNotificationCommand
        {
            get;
            internal set;
        }

        #endregion

        public void Load(String username)
        {
            this.username = username;
            Loading = true;
            HttpGet httpGetLoadFriendNotifications = new HttpGet(GlobalHelper.Url + String.Format("GetFriendNotification/{0}/{1}", 
                UserHelper.Token, username), typeof(GetFriendNotificationResult));
            httpGetLoadFriendNotifications.FinishedGet += new EventHandler(httpGetLoadFriendNotifications_FinishedGet);
        }

        private void httpGetLoadFriendNotifications_FinishedGet(object sender, EventArgs e)
        {
            try
            {
                HttpGet Get = sender as HttpGet;
                if (Get != null && (Get.resultObject as GetFriendNotificationResult) != null)
                {
                    GetFriendNotificationResult result = (Get.resultObject as GetFriendNotificationResult);
                    Notification = result.Notification != null ? result.Notification : new Notification() { FriendUsername = username, Longitude = 0, Latitude = 0, Range = 1.0};
                    System.Windows.Deployment.Current.Dispatcher.BeginInvoke(() => { if (FriendNotificationPage != null) FriendNotificationPage.SetPosition(); });
                }
            }
            catch (Exception ex) { }
            finally { Loading = false; }
        }

        public override void Update()
        {
            base.Update();
        }

        /// <summary>
        /// Voeg een locatie notificatie toe op een vriend
        /// </summary>
        private void Save()
        {
            Loading = true;

            NumberFormatInfo numberFormatter = new CultureInfo("nl-NL").NumberFormat;

            HttpGet httpGetAddFriendNotifications = new HttpGet(GlobalHelper.Url + String.Format("AddFriendNotification/{0}/{1}/{2}/{3}/{4}",
               UserHelper.Token, Notification.FriendUsername, Convert.ToString(Notification.Latitude, numberFormatter), 
               Convert.ToString(Notification.Longitude, numberFormatter), Convert.ToString(Notification.Range, numberFormatter)), 
               typeof(AddFriendNotificationResult));
            httpGetAddFriendNotifications.FinishedGet += new EventHandler(httpGetAddFriendNotifications_FinishedGet);
        }

        private void httpGetAddFriendNotifications_FinishedGet(object sender, EventArgs e)
        {
            try
            {
                HttpGet get = sender as HttpGet;
                if (get != null && (get.resultObject as AddFriendNotificationResult) != null)
                {
                    AddFriendNotificationResult result = get.resultObject as AddFriendNotificationResult;
                    switch (result.Result)
                    {
                        case AddFriendNotificationResultEnum.LatOrLonIsEmpty:
                            GlobalHelper.ShowMessage("Error", "No location defined.");
                            break;
                        case AddFriendNotificationResultEnum.NotificationAlreadyRegistred:
                            GlobalHelper.ShowMessage("Error", "Notification already registered.");
                            break;
                        case AddFriendNotificationResultEnum.RangeToBig:
                            GlobalHelper.ShowMessage("Error", "The specified range is to large.");
                            break;
                        case AddFriendNotificationResultEnum.Succes:
                            GlobalHelper.ShowMessage("", "Notification succesfully added.");
                            break;
                        case AddFriendNotificationResultEnum.UserNotExists:
                            GlobalHelper.ShowMessage("Error", "User doesn't exists.");
                            break;
                    }
                }
            }
            catch (Exception ex) { }
            finally { Loading = false; }
        }
    }
}
